from typing import Optional,List
from sqlmodel import Field, SQLModel,Relationship

class HeroTeamLink(SQLModel,table=True):
    #关系表
    hero_id:Optional[int] = Field(default=None,foreign_key="hero.id",primary_key=True)
    team_id:Optional[int] = Field(default=None,foreign_key="team.id",primary_key=True)

class Hero(SQLModel,table=True):
    id:Optional[int] = Field(default=None,primary_key=True)
    name:str = Field(index=True)
    age:int
    # 通过声明链接表来支持表之间的多对多关系
    teams:Optional[List["Team"]] = Relationship(back_populates="heroes",link_model=HeroTeamLink)

class Team(SQLModel,table=True):
    id:Optional[int] = Field(default=None,primary_key=True)
    name:str = Field(index=True)
    heroes:Optional[List[Hero]] = Relationship(back_populates="teams",link_model=HeroTeamLink)


